from flask import Flask
from flask import current_app,send_from_directory
from random_code import ImageChar
from io import StringIO,BytesIO
app = Flask(__name__)


@app.route("/code")
def code():
    ic = ImageChar(fontColor=(100,211, 90))
    strs,code_img = ic.randChinese(4)
    #session[current_app.config.get('S_RECAPTCHA')]= strs
    #buf = StringIO()
    try:
        buf = StringIO()
        code_img.save(buf,'JPEG',quality=70)
    except:
        buf = BytesIO()
        code_img.save(buf,'JPEG',quality=70)
    buf_str = buf.getvalue()
    response = current_app.make_response(buf_str)
    response.headers['Content-Type'] = 'image/jpeg'
    response.headers['x-verifying-code'] = strs
    
    return response
    #return send_from_directory("./",strs)
    '''
    with open(strs,"r") as image:
        data = image
        print(data)
        response = current_app.make_response(data)
        response.headers['Content-Type'] = 'image/jpeg'
        response.headers['x-verifying-code'] = strs
    return response
    '''



if "__main__" == __name__:
    app.run(debug=True)
